package com.wyd.wenyongdaAdmin.xo.admin.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyd.wenyongdaAdmin.commons.model.admin.role.dto.RoleMenuDTO;
import com.wyd.wenyongdaAdmin.commons.model.admin.role.entity.RoleMenuEntity;
import com.wyd.wenyongdaAdmin.xo.admin.role.mapper.RoleMenuMapper;
import com.wyd.wenyongdaAdmin.xo.admin.role.service.IRoleMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuEntity> implements IRoleMenuService {

    private static final Logger log = LoggerFactory.getLogger(RoleMenuServiceImpl.class);


    @Override
    public List<Long> getRoleMenuIdsByRoleId(Long roleId) {
        List<RoleMenuEntity> roleMenuEntities = this.baseMapper.selectList(new QueryWrapper<RoleMenuEntity>().eq("role_id", roleId).select("menu_id"));
        List<Long> roleMenuIDs = new ArrayList<>();
        for (RoleMenuEntity roleMenuEntity : roleMenuEntities) {
            roleMenuIDs.add(roleMenuEntity.getMenuId());
        }
        return roleMenuIDs;
    }

    @Override
    public Integer updateRoleMenuIdsByRoleId(Long roleId, List<Long> menuIds) {
        List<Long> dbids = this.getRoleMenuIdsByRoleId(roleId);
        // 如果 修改的menuIds 与 dbids 相同 则直接返回1
        List<Long> existMenuIds = new ArrayList<>(dbids);
        List<Long> newMenuIds = new ArrayList<>(menuIds);
        existMenuIds.sort(Comparator.naturalOrder());
        newMenuIds.sort(Comparator.naturalOrder());
        if (existMenuIds.toString().equals(newMenuIds.toString())) {
            return 1;
        }
        List<Long> deleteMenuIds = new ArrayList<>(dbids);
        // 拿出需要删除的
        deleteMenuIds.removeAll(menuIds);
        List<Long> insertMenuIds = new ArrayList<>(menuIds);
        // 拿出需要新增的
        insertMenuIds.removeAll(dbids);
        int res = 0;
        // 有需要删除的
        if (deleteMenuIds.size() > 0) {
            int i = this.baseMapper.deleteBatchByMenuIds(deleteMenuIds);
            if(i > 0) {
                res += i;
            }
            // 有需要新增的
            if (insertMenuIds.size() > 0) {
                List<RoleMenuDTO> roleMenuDTOs = new ArrayList<>();
                RoleMenuDTO roleMenuDTO = null;
                for (Long menuId : insertMenuIds) {
                    roleMenuDTO = new RoleMenuDTO().setMenuId(menuId).setRoleId(roleId).setCreateTime(new Timestamp(System.currentTimeMillis()));
                    roleMenuDTOs.add(roleMenuDTO);
                }
                int j = this.baseMapper.insertBatch(roleMenuDTOs);
                if (j > 0) {
                    res += j;
                    return res;
                }
            } else {
                return res;
            }
            // 没有需要删除的
        } else {
            if (insertMenuIds.size() > 0) {
                List<RoleMenuDTO> roleMenuDTOs = new ArrayList<>();
                RoleMenuDTO roleMenuDTO = null;
                for (Long menuId : insertMenuIds) {
                    roleMenuDTO = new RoleMenuDTO().setMenuId(menuId).setRoleId(roleId).setCreateTime(new Timestamp(System.currentTimeMillis()));
                    roleMenuDTOs.add(roleMenuDTO);
                }
                int j = this.baseMapper.insertBatch(roleMenuDTOs);
                if (j > 0) {
                    res += j;
                    return res;
                }
            }
        }
        return 0;
    }
}
